/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ReciteTestMethodBase.h"

ReciteTestMethodBase::ReciteTestMethodBase(WordBrowser* display, QWidget* parent):
	QWidget(parent),
	textDisplay(display)
{
	mFont.setPointSize(12);
	mFont.setBold(true);

	nextButton = new QPushButton(tr("Next [C]"), this);
	nextButton->setIcon(QIcon("./pixmaps/forward-256.png"));
	nextButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	nextButton->setMinimumSize(120,60);
	nextButton->setMaximumSize(140,80);
	nextButton->setFont(mFont);
	//nextButton->setPalette(QPalette(QColor(Qt::yellow)));
	//nextButton->setAutoFillBackground(true);
	nextButton->hide();

	//testMethodWidget = new QWidget(this);
	testMethodGrid = new QGridLayout(this);
	testMethodGrid->setContentsMargins(0,0,0,0);
	testMethodGrid->addWidget(nextButton, 1, 1);

	nextButtonShortCut = new QShortcut(QKeySequence(Qt::Key_C),  this);
	QObject::connect(nextButtonShortCut,SIGNAL(activated()),nextButton,SIGNAL(clicked()));
	QObject::connect(nextButton,SIGNAL(clicked()),this,SLOT(nextButtonClicked()));
}

ReciteTestMethodBase::~ReciteTestMethodBase()
{
}

void ReciteTestMethodBase::beginTest()
{
	cout<<"ReciteTestMethodBase::beginTest"<<endl;
	show();
	textDisplay->getTextBrowser()->setReadOnly(true);
	displayHtmlString.clear();

	float newLevel = WM.getCurWord()->getMemoryLevel();
	if (newLevel>1)
		newLevel=1;
	//emit memoryLevelChangedTo(int(100*newLevel));
	emit updateMemoryLevel();
}

void ReciteTestMethodBase::endTest()
{
	cout<<"ReciteTestMethodBase::endTest"<<endl;
	resetDynamicButtons();
	hide();
	emit testFinished();
	//testMethodWidget->hide();
	//throw recite_test_err_test_finished;
}

void ReciteTestMethodBase::showNextButton()
{
	//cout<<"ReciteTestMethodBase::showNextButton"<<endl;
	nextButton->show();
	nextButtonShortCut->setEnabled(true);
}

void ReciteTestMethodBase::insertCensoredXDICTExp()
{
	Word* word = WM.getCurWord();
	if(word)
	{
		QString after;
		QString spellExp;
		QString English = word->getEnglish();

		spellExp = word->getExplanation(DictNames::XDICTDict);
		int startSoundMark = spellExp.indexOf('[');
		int endSoundMark = spellExp.indexOf(']');
		spellExp.remove(startSoundMark+1, endSoundMark - startSoundMark - 1);
		//after.clear();
		//for(int i=0; i<English.size(); i++)
		//	after.append("*");
		after = "??????";
		spellExp.replace(English, after, Qt::CaseInsensitive);
		displayHtmlString.append(spellExp + QString("<br>"));
		//textDisplay->insertHtml(spellExp);
		//textDisplay->insertHtml("<br>");
	}
}

void ReciteTestMethodBase::insertCensoredLongmanExp()
{
	Word* word = WM.getCurWord();
	if(word)
	{
		QString after;
		QString spellExp;
		QString English = word->getEnglish();

		spellExp = word->getExplanation(DictNames::LongmanDict);
		spellExp.remove(QString::fromUtf8("·"));

		after = "??????";
		//after.clear();
		//for(int i=0; i<English.size(); i++)
		//	after.append("*");
		spellExp.replace(English, after, Qt::CaseInsensitive);

/*		while (spellExp.indexOf('[') - spellExp.indexOf(']') > 0)
		{
			int startSoundMark = spellExp.indexOf('[');
			int endSoundMark = spellExp.indexOf(']');
			spellExp.remove(startSoundMark+1, endSoundMark - startSoundMark - 1);
		}
*/
		int sensitiveLength;
		if (English.size() > 5)
			sensitiveLength = int(English.size()*0.7);
		else
			sensitiveLength = int(English.size()-1);

		English = English.left(sensitiveLength);
		after = after.left(sensitiveLength);
		spellExp.replace(English, after, Qt::CaseInsensitive);

		English = English.right(sensitiveLength);
		spellExp.replace(English, after, Qt::CaseInsensitive);

		displayHtmlString.append(spellExp + QString("<br>"));
		//textDisplay->insertHtml(spellExp);
		//textDisplay->insertHtml("<br>");
	}
}

void ReciteTestMethodBase::resetDynamicButtons()
{
	//testMethodWidget->hide();
	nextButton->hide();
	nextButtonShortCut->setEnabled(false);
}

void ReciteTestMethodBase::forget()
{
	WM.forget();
	showNextButton();

	float newLevel = WM.getCurWord()->getMemoryLevel();
	if (newLevel>1)
		newLevel=1;
	//emit memoryLevelChangedTo(int(100*newLevel));
	emit updateMemoryLevel();
}

void ReciteTestMethodBase::remember()
{
	WM.remember();
	showNextButton();

	float newLevel=WM.getCurWord()->getMemoryLevel();
	if (newLevel>1)
		newLevel=1;
	//emit memoryLevelChangedTo(int(100*newLevel));
	emit updateMemoryLevel();
}

void ReciteTestMethodBase::nextButtonClicked()
{
	//cout<<"ReciteTestMethodBase::nextButtonClicked"<<endl;
	endTest();
}
